package com.sdcs.server.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sdcs.server.model.Teacher;

@Component
public class TeacherDaoImpl {
	@Autowired
	private SessionFactory sessionFactory;
	
	public Teacher login(String name, String password) {
		Session sess = null;
		Transaction tx = null;
		Teacher teacher = null;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Teacher where teacher_name = ? and password = ?";
			List<Teacher> list = sess.createQuery(hql, Teacher.class).setParameter(0, name).setParameter(1, password).getResultList();
			
			if (!list.isEmpty())
				teacher = list.get(0);
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return teacher;
	}
	
	public void delete(String id){
		Session sess = null;
		Transaction tx = null;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = " delete Teacher where teacher_id = ?"; 
			sess.createQuery(hql).setParameter(0, id).executeUpdate();
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		} 
	}
	
	public void save(Teacher object) {
		Session sess = null;
		Transaction tx = null;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			sess.save(object);
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
	}
	
	public void update(Teacher object) {
		Session sess = null;
		Transaction tx = null;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			sess.update(object);
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
	}
	
	public Teacher getById(String id) {
		Session sess = null;
		Transaction tx = null;
		Teacher teacher = null;
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Teacher where teacher_id = ?";
			List<Teacher> list = sess.createQuery(hql, Teacher.class).setParameter(0, id).getResultList();
			
			if (!list.isEmpty())
				teacher = list.get(0);
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return teacher;
	}
	
	public List<Teacher> getAll(int pageIndex, int pageSize) {
		Session sess = null;
		Transaction tx = null;
		List<Teacher> list = new ArrayList<Teacher>();
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Teacher";
			list = sess.createQuery(hql, Teacher.class).setFirstResult(pageIndex).setMaxResults(pageSize).getResultList();
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return list;
	}
	
	public int getTotalCount() {
		Session sess = null;
		Transaction tx = null;
		int rowCount = 0;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Teacher";
			rowCount = sess.createQuery(hql).getResultList().size();
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return rowCount;
	}
}
